<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fix User Passwords</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body {
            padding: 20px;
        }
        .container {
            max-width: 800px;
        }
        .card {
            margin-bottom: 20px;
        }
        .alert {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="mb-4">Fix User Passwords</h1>
        
        <div class="alert alert-success" id="successAlert">
            <strong>Success!</strong> <span id="successMessage"></span>
        </div>
        
        <div class="alert alert-danger" id="errorAlert">
            <strong>Error!</strong> <span id="errorMessage"></span>
        </div>
        
        <div class="card">
            <div class="card-header">Fix All Passwords</div>
            <div class="card-body">
                <p>This will set a default password for all users who don't have a password.</p>
                <form id="fixPasswordsForm">
                    <div class="form-group">
                        <label for="defaultPassword">Default Password</label>
                        <input type="password" class="form-control" id="defaultPassword" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Fix Passwords</button>
                </form>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">Reset Password for a User</div>
            <div class="card-body">
                <form id="resetPasswordForm">
                    <div class="form-group">
                        <label for="identifier">Username or Email</label>
                        <input type="text" class="form-control" id="identifier" required>
                    </div>
                    <div class="form-group">
                        <label for="newPassword">New Password</label>
                        <input type="password" class="form-control" id="newPassword" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Reset Password</button>
                </form>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">View All Users</div>
            <div class="card-body">
                <button id="viewUsersBtn" class="btn btn-info">View Users</button>
                <div id="usersList" class="mt-3"></div>
            </div>
        </div>
        
        <div class="mt-4">
            <a href="index.html" class="btn btn-secondary">Back to Home</a>
        </div>
    </div>
    
    <script>
        // 获取基础URL
        const baseUrl = window.location.pathname.includes('/file-transfer') ? '/file-transfer' : '';
        
        // 显示成功消息
        function showSuccess(message) {
            document.getElementById('successMessage').textContent = message;
            document.getElementById('successAlert').style.display = 'block';
            setTimeout(() => {
                document.getElementById('successAlert').style.display = 'none';
            }, 5000);
        }
        
        // 显示错误消息
        function showError(message) {
            document.getElementById('errorMessage').textContent = message;
            document.getElementById('errorAlert').style.display = 'block';
            setTimeout(() => {
                document.getElementById('errorAlert').style.display = 'none';
            }, 5000);
        }
        
        // 修复所有密码
        document.getElementById('fixPasswordsForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const defaultPassword = document.getElementById('defaultPassword').value;
            
            try {
                const response = await fetch(`${baseUrl}/debug/fix-passwords?defaultPassword=${defaultPassword}`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                if (data.success) {
                    showSuccess(data.message);
                } else {
                    showError(data.message);
                }
            } catch (error) {
                showError('An error occurred: ' + error.message);
            }
        });
        
        // 重置用户密码
        document.getElementById('resetPasswordForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const identifier = document.getElementById('identifier').value;
            const newPassword = document.getElementById('newPassword').value;
            
            try {
                const response = await fetch(`${baseUrl}/debug/reset-password?identifier=${encodeURIComponent(identifier)}&newPassword=${encodeURIComponent(newPassword)}`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                if (data.success) {
                    showSuccess(data.message);
                } else {
                    showError(data.message);
                }
            } catch (error) {
                showError('An error occurred: ' + error.message);
            }
        });
        
        // 查看所有用户
        document.getElementById('viewUsersBtn').addEventListener('click', async () => {
            try {
                const response = await fetch(`${baseUrl}/debug/users`);
                const data = await response.json();
                
                if (data.success) {
                    const users = data.data;
                    let html = '<table class="table table-striped">';
                    html += '<thead><tr><th>Username</th><th>Email</th><th>Has Password</th></tr></thead>';
                    html += '<tbody>';
                    
                    users.forEach(user => {
                        html += `<tr>
                            <td>${user.username}</td>
                            <td>${user.email || ''}</td>
                            <td>${user.password ? 'Yes' : 'No'}</td>
                        </tr>`;
                    });
                    
                    html += '</tbody></table>';
                    document.getElementById('usersList').innerHTML = html;
                } else {
                    showError(data.message);
                }
            } catch (error) {
                showError('An error occurred: ' + error.message);
            }
        });
    </script>
</body>
</html> 